
<?php
    header("Access-Control-Allow-Origin:*"); 
	require_once("connect_sql.php");
	
	//echo "start";
	//var_dump($queryType);
	if(!empty($_GET['type'])) {
		$queryType = $_GET['type'];
	} else {
		$queryType = "";
		echo "failed";
		exit;
	}
	if($queryType == "customer") {
		$board = $_GET['board'];
		if(!checkInput($board)) exit;
		
		$sql = "select distinct id,abbreviate from customer where id in (select customer from project where board=\"$board\")";
		
		$result = mysql_query($sql);
		//var_dump($result);
		if(!$result) echo mysql_error();
		
		$i = 0;
		while($row = mysql_fetch_array($result)) {
			$idArray[$i] = trim($row['id']);
			$dataArray[$i++] = trim($row['abbreviate']);
		}
		if($i != 0) printf("%s|%s", join(',', $idArray), join(',', $dataArray));
		exit;
	} else if($queryType == "business") {
		$board = $_GET['board'];
		$customer = $_GET['customer'];
		if(!checkInput($board) || !checkInput($customer)) exit;
		
		$sql = "select distinct business from project where board=\"$board\" and customer=\"$customer\" order by business";
		
		$result = mysql_query($sql);
		if(!$result) echo mysql_error();
		
		$i = 0;
		while($row = mysql_fetch_array($result)) {
			$business = trim($row['business']);
			$dataArray[$i++] = $business;
		}
		if($i != 0) echo join(',', $dataArray);
		exit;
	} else if($queryType == "board") {
		$sql = "select distinct board from project order by board";
		$result = mysql_query($sql);
		if(!$result) echo mysql_error();
		$i = 0;
		while($row = mysql_fetch_array($result)) {
			$board = trim($row['board']);
			$dataArray[$i++] = $board;
		}
		if($i != 0) echo join(',', $dataArray);
		exit;
	}
	
	function checkInput($val) {
		if($val && $val != "empty") {
			return true;
		}
		else {
			echo "empty";
			return false;
		}
	}
?>